package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.service.CategoryService;
import com.zrrd.reggie.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){
        Page<Category> pageInfo=new Page<>(page,pageSize);
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("sort");
        categoryService.page(pageInfo,queryWrapper);
        return R.sucess(pageInfo);
    }
    @PostMapping
    public R<String> add(@RequestBody Category category){
        boolean save=categoryService.save(category);
        if(save){
            return R.sucess("添加成功");
        }else {
            return R.error("添加失败");
        }
    }
    @PutMapping
    public R<String> update(@RequestBody Category category){
        boolean update=categoryService.updateById(category);
        if(update){
            return R.sucess("修改成功");
        }else {
            return R.error("修改失败");
        }
    }
    @DeleteMapping
    public R<String> delete(Long id){
        boolean delete=categoryService.removeById(id);
        if(delete){
            return R.sucess("删除成功");
        }else {
            return R.error("删除失败");
        }
    }
    @GetMapping("/list")
    public R<List<Category>> getCategoryList(Integer type){
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        if(type!=null){
            queryWrapper.eq("type",type);
            queryWrapper.orderByDesc("sort");
        }
        return R.sucess(categoryService.list(queryWrapper));
    }

}
